import socket
import os


class Client:

    def __init__(self):
        self.ss=socket.socket()
        self.con=('10.2.1.60',8888)
        self.dir=os.path.dirname(os.path.abspath(__file__))

    def start(self):
        self.ss.connect(self.con)
        self.file_description_and_send()

    def file_description_and_send(self):
        inp=input('please input like：post|file_name').strip()
        cmd,file_name=inp.split('|')
        path=os.path.join(self.dir,file_name)
        file_size=os.stat(path).st_size
        file_info='{}|{}|{}'.format(cmd,file_name,file_size)
        self.ss.send(file_info.encode())
        self.send_msg(file_size,path)

    def send_msg(self,file_size,path):
        has_sent=0
        with open(path,'rb') as f:
            while has_sent !=int(file_size):
                data=f.read(1024)
                self.ss.send(data)
                has_sent+=len(data)
                print('已经上传{:.2}'.format(has_sent/file_size))
        print('-------------------')
        print('完成上传')




if __name__=='__main__':
    c=Client()
    c.start()


